package com.maochang.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.maochang.mapper.PsInspectiontaskDetailMapper;
import com.maochang.mapper.PsSolvetaskDetailMapper;
import com.maochang.pojo.*;
import com.maochang.pojo.util.ChuAnSuoKuAng;
import com.maochang.pojo.util.Result1;
import com.maochang.pojo.util.ZhuRenWUHeXunJianMingXi;
import com.maochang.pojo.util.ZhuRenWuXiaoQueMingXi;
import com.maochang.service.PsInspectorDetailService;
import com.maochang.service.PsUserService;
import com.maochang.service.XunJianRenWuZhiDingYuFenPeiService;
import com.maochang.service.xiaoQuZhiDingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/XunJiAnRenWu")
public class XunJiAnRenWuDeZhiDingYuFenPei {

    @Resource
    private XunJianRenWuZhiDingYuFenPeiService xunJianRenWuZhiDingYuFenPeiService;

    @Resource 
    private PsUserService  psUserServiceImpl;

    @Resource
    private PsInspectorDetailService PsInspectorDetailServiceImpl;
    @Resource
    private PsSolvetaskDetailMapper psSolvetaskDetailMapper;
    @Resource
    private PsInspectiontaskDetailMapper psInspectiontaskDetailMapper;
    @Resource
    private xiaoQuZhiDingService xiaoQuZhiDingImpl;
    /**
     * 巡检的制定 于分配
     * @return
     * @throws IOException
     */
    @GetMapping("/zhiDingFenPei")
    public ModelAndView index2() throws IOException {
        return  new ModelAndView("xunjianguanli/XunJianRenWuDeZhiDingFenPei");
    }
    /**
     * 缺陷查询
     * @return
     * @throws IOException
     */
    @GetMapping("/QuXianChaXun")
    public ModelAndView QuXianChaXun() throws IOException {
        return  new ModelAndView("xunjianguanli/QuXianChaXun");
    }
    /**
     * 表格数据
     * @param aa
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/ChaShuJu")
    public Result1 ChaJson(PsInspectiontaskMain aa,@RequestParam("page") int current,@RequestParam("limit") int size){
      Page<PsInspectiontaskMain> PsInList= xunJianRenWuZhiDingYuFenPeiService.ChaXjZhuBiAo(aa,current,size);
      return Result1.success1(PsInList);
    }

    /**
     * 查询穿梭框数据
     * @return
     */
    @PostMapping("/ChaChuAnSuoKuang")
    public List<ChuAnSuoKuAng>  chuAnSuoKuAng(){
        System.out.println("aa");
        //自己生成的 集合  list
        List<ChuAnSuoKuAng> cha=new ArrayList<>();
        List<PsUser>  psUsersLsit= psUserServiceImpl.ChaXunJiAn();
        for (PsUser i : psUsersLsit) {
            //自己设置的类
             ChuAnSuoKuAng chuAnSuoKuAng=new ChuAnSuoKuAng();
             chuAnSuoKuAng.setValue(i.getId().toString());
             chuAnSuoKuAng.setTitle(i.getUserName());
             cha.add(chuAnSuoKuAng);
        }
        return  cha;
    }

    /**
     * 分配任务   插入信息到   任务巡检员信息表
     * @param id
     * @param aa
     * @param httpSession
     * @return
     */
    @PostMapping("/FenPeiRenWu")
        public Result1  FenPeiRenWu(Integer id,@RequestBody List<ChuAnSuoKuAng> aa,HttpSession httpSession){
        //当前用户(创建者)
        //System.out.println("你好a");
        PsInspectorDetail psInspectorDetail= new PsInspectorDetail();
        PsUser  yongHu=null;
        int  row=0;
        for(ChuAnSuoKuAng chuAnSuoKuAng:aa){
            //主任务id
            psInspectorDetail.setTaskId(id);
            //巡检员的code
            psInspectorDetail.setInspectorCode(chuAnSuoKuAng.getValue());
            //巡检员 name
            psInspectorDetail.setInspectorName(chuAnSuoKuAng.getTitle());
            //创建者
            yongHu= (PsUser) httpSession.getAttribute("loginUser");
            psInspectorDetail.setCreatedBy(yongHu.getId().toString());
            //创建时间
            LocalDateTime nowTime= LocalDateTime.now();
            psInspectorDetail.setCreationTime(nowTime);
            //最后修改时间
            psInspectorDetail.setLastUpdateTime(nowTime);
            //添加巡检信息
            PsInspectorDetailServiceImpl.xjMingXiAdd(psInspectorDetail);
            row++;
        }
        Result1 result1=new Result1();
        if(row>0){
            //改掉主表状态
            boolean  row2 =xunJianRenWuZhiDingYuFenPeiService.gaiZhuAngTai(id);
            if(row2==true){
                result1.setMsg("分配成功");
            }
        }
        return  result1;
    };
    /**
     * 取消巡检
     * @param id
     * @return
     */
    @PostMapping("/QuXiAoXunJiAn")
    public  Result1 quxiAoXunJian(Integer id){
        Result1 result1=new Result1();
        boolean row=xunJianRenWuZhiDingYuFenPeiService.QuXiAoXunJiAn(id);
        if(row){
            //是否取消
            result1.setMsg("取消成功");
        }
        return  result1;
    };

    /**
     * 添加巡检信息
     * @param xi
     * @param session
     * @return
     */
    @PostMapping("/AddXunJiAnRenWu")
    public  Result1 AddXunJiAnRenWu(@RequestBody ZhuRenWUHeXunJianMingXi xi,HttpSession session){
        Result1 result1=new Result1();
        boolean row=xunJianRenWuZhiDingYuFenPeiService.addXunJiAn(xi,session);
        if(row){
            //是否取消
            result1.setMsg("添加巡检信息成功");
            //巡检员明细表
        }
        return  result1;
    };

    /**
     * 查任务
     * @param id
     * @return
     */
    @GetMapping("/ChaDanXunJiAnRenWu")
    public  PsInspectiontaskMain ChaDanXunJiAnRenWu(String id){
        PsInspectiontaskMain row=xunJianRenWuZhiDingYuFenPeiService.ChaRenWu(id);
        return  row;
    };
    /**
     * 查巡检员
     */
    @GetMapping("/ChaXunJianYuan")
    public  List<PsInspectorDetail> ChaXunJianYuan(String id){
        List<PsInspectorDetail> row=xunJianRenWuZhiDingYuFenPeiService.CaXJiAnYUn(id);
        System.out.println(row);
        return  row;
    };
    @GetMapping("/ShanChuZiBiao")
    @Transactional
    public  boolean ShanChuZiBiao(String id){
        QueryWrapper<PsSolvetaskDetail> ac= Wrappers.query();
        ac.eq("inspectionTaskDelId",id);
        int row=psSolvetaskDetailMapper.delete(ac);
        if(row>0){
            //改变巡检任务子表状态
            PsInspectiontaskDetail aa=new PsInspectiontaskDetail();
            aa.setIsSolve(0);
            QueryWrapper<PsInspectiontaskDetail> ca= Wrappers.query();
            ca.eq("id",id);
            int falg=  psInspectiontaskDetailMapper.update(aa,ca);
            if(falg>0){
                return true;
            }
            return  false;
        }
        return  false;
    };

    @PostMapping("/XiaoQuMessageXiuGai")
    public  boolean XiaoQuMessageXiuGai(Integer id, @RequestBody ZhuRenWuXiaoQueMingXi xi, HttpSession httpSession){
        System.out.println("=====================传值成功=========================="+id);
        //修改 主菜单内容 条件是id
        boolean aa=xiaoQuZhiDingImpl.xiuGai(id,xi,httpSession);
        return  aa==true?true:false;
    };
}
